package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws Exception {

        Class cls=Class.forName("reflect.Person");
        Constructor c=cls.getConstructor();
        Object obj=c.newInstance();

        //使用放射机制调用sayHello方法
        //1. first throw class name get method
        Method method=cls.getMethod("sayHello");
        //2. invoke method used to start the method
        method.invoke(obj);

        Method method1=cls.getMethod("getName");
        //当前方法有返回值时，可以用objet类型的变量来接收数据
        Object returenValue=method1.invoke(obj);
        System.out.println("returenValue:"+returenValue);


        Scanner scan=new Scanner(System.in);
        System.out.println("请输入一个类名：");
        String className=scan.nextLine();
        System.out.println("请输入一个方法名：");
        String methodName =scan.nextLine();
        Class cls2=Class.forName(className);
        Constructor c2=cls2.getConstructor();
        Object obj2=c2.newInstance();
        Method method2=cls2.getMethod(methodName);
        method2.invoke(obj2);










    }
}
